Mybatis-Note

  • mybatis 中批量可选择性的 update 不同 table 中的 data

    • 方法:

      1. 前端传入表名 和 List<LinkedHashMap> 类型的 valueList

        1
        2
        private String category;
        private List<LinkedHashMap> itemList;
      2. 中台根据不同的表名调用不同 mapper 类的 update 语句

      3. mappers包中的对应xml中编写update语句模板

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        <update id="updateListSelective" parameterType="java.util.List" >
        <foreach collection="listOfUpdate" index="index" item="item" separator=";">
        update counter_item
        <set>
        <if test="item.price != null">
        price = #{item.price,jdbcType=DECIMAL},
        </if>
        <if test="item.count != null">
        count = #{item.count,jdbcType=INTEGER},
        </if>
        <if test="item.advisePrice != null">
        advise_price = #{item.advisePrice,jdbcType=DECIMAL},
        </if>
        </set>
        where
        counter_id = #{item.counterId,jdbcType=INTEGER}
        and
        barcode = #{item.barcode,jdbcType=VARCHAR}
        </foreach>
        </update>

        注意: parameterTypeList, <foreach>标签中的字段前面要加列表内容名(即item), mybatis会自动识别List<LinkedHashMap>中的字段

  • Mybatis 中 collection 中 List 的默认值为: list